package com.ruoyi.mall.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.mall.domain.PmsAlbum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.PmsBrandMapper;
import com.ruoyi.mall.domain.PmsBrand;
import com.ruoyi.mall.service.IPmsBrandService;
import org.springframework.util.StringUtils;

/**
 * 商品品牌Service业务层处理
 * 
 * @author fdk
 * @date 2023-09-09
 */
@Service
public class PmsBrandServiceImpl implements IPmsBrandService 
{
    @Autowired
    private PmsBrandMapper pmsBrandMapper;

    /**
     * 查询商品品牌
     * 
     * @param id 商品品牌主键
     * @return 商品品牌
     */
    @Override
    public PmsBrand selectPmsBrandById(Long id)
    {
        return pmsBrandMapper.selectPmsBrandById(id);
    }

    /**
     * 查询商品品牌列表
     * 
     * @param pmsBrand 商品品牌
     * @return 商品品牌
     */
    @Override
    public List<PmsBrand> selectPmsBrandList(PmsBrand pmsBrand)
    {
        return pmsBrandMapper.selectPmsBrandList(pmsBrand);
    }

    /**
     * 新增商品品牌
     * 
     * @param pmsBrand 商品品牌
     * @return 结果
     */
    @Override
    public int insertPmsBrand(PmsBrand pmsBrand)
    {
        String brandName = pmsBrand.getName();

        if (StringUtils.isEmpty(brandName)){
            throw new RuntimeException("商品名称不能为空，请指定商品品牌名称");
        }

        if (StringUtils.isEmpty(pmsBrand.getPinyin())){
            throw new RuntimeException("品牌拼音不能为空，请指定商品品牌的拼音");
        }
        int rows = pmsBrandMapper.selectPmsBrandByName(brandName);

        if (rows != 0){
            throw new RuntimeException("品牌名称已经存在，不可以新增");
        }

        return pmsBrandMapper.insertPmsBrand(pmsBrand);
    }

    /**
     * 修改商品品牌
     * 
     * @param pmsBrand 商品品牌
     * @return 结果
     */
    @Override
    public int updatePmsBrand(PmsBrand pmsBrand)
    {
        //1.获取修改的商品品牌的名称
        String updateName = pmsBrand.getName();
        //2.判断修改的名称是否为空
        if (StringUtils.isEmpty(updateName)){
            throw new RuntimeException("修改的品牌名称不能为空");
        }
        if (StringUtils.isEmpty(pmsBrand.getPinyin())){
            throw new RuntimeException("修改的品牌拼音不能为空");
        }
//        PmsBrand brand = new PmsBrand();
//        brand.setName(updateName);
//        pmsBrandMapper.selectPmsBrandList(brand);
        PmsBrand brand = pmsBrandMapper.selectBrandByName(updateName);
        if (brand != null){
            if (brand.getName().equals(updateName) && brand.getId() != pmsBrand.getId()){
                throw new RuntimeException("新修改的品牌的名称已经存在，修改失败");
            }
        }
        pmsBrand.setGmtModified(new Date());
        return pmsBrandMapper.updatePmsBrand(pmsBrand);
    }

    /**
     * 批量删除商品品牌
     * 
     * @param ids 需要删除的商品品牌主键
     * @return 结果
     */
    @Override
    public int deletePmsBrandByIds(Long[] ids)
    {
        if(ids.length == 0){
            throw new RuntimeException("至少要选中一条数据进行删除");
        }
        return pmsBrandMapper.deletePmsBrandByIds(ids);
    }

    /**
     * 删除商品品牌信息
     * 
     * @param id 商品品牌主键
     * @return 结果
     */
    @Override
    public int deletePmsBrandById(Long id)
    {
        return pmsBrandMapper.deletePmsBrandById(id);
    }

}
